package site.xuqing.autohelperscript.script;

import android.accessibilityservice.AccessibilityService;
import android.view.accessibility.AccessibilityNodeInfo;

import java.util.ArrayList;
import java.util.List;

import site.xuqing.autohelper.StepScript;
import site.xuqing.autohelper.bean.StepBean;
import site.xuqing.autohelper.bean.StepCallbackBean;
import site.xuqing.autohelper.listener.OnStepListener;
import site.xuqing.autohelper.util.StepExecutor;
import site.xuqing.autohelper.util.StringUtil;

/**
 * 输入脚本
 * @author XuQing
 */
public class StepScriptInput extends StepScript {
    @Override
    public void doStep(AccessibilityService service, StepBean stepBean, StepExecutor stepExecutor, OnStepListener onStepListener) throws Exception {
        List<AccessibilityNodeInfo> edits=new ArrayList<>();
        stepExecutor.findNodesByClassName(service.getRootInActiveWindow(),"android.widget.EditText",edits);
        if (edits.size()>0){
            AccessibilityNodeInfo edit=edits.get(0);
            StepCallbackBean stepCallbackBean=doSyncCallback(onStepListener,new StepCallbackBean().setErrorCode(10002));
            if (stepCallbackBean!=null&& !StringUtil.isEmptyString(stepCallbackBean.getMessage())) {
                stepExecutor.paste(edit, stepCallbackBean.getMessage());
            }
        }else{
            throw new Exception("未找到输入框");
        }
    }
}
